package com.hst.capacity.infra.persistence.redis;


import com.hst.capacity.domain.repository.redis.CacheRedis;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class CacheRedisImpl implements CacheRedis {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void writeOrderPriceValue(String keySuffix, String value) {
        String key = String.format(ORDER_PRICE_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, value, 600, TimeUnit.SECONDS);
    }

    @Override
    public String getOrderPriceValue(String keySuffix) {
        String key = String.format(ORDER_PRICE_KEY, keySuffix);
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }

    @Override
    public void writeOrderDriverValue(String keySuffix, String value) {
        String key = String.format(ORDER_DRIVER_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, value, 10, TimeUnit.SECONDS);
    }

    @Override
    public void clearOrderDriverValue(String keySuffix) {
        String key = String.format(ORDER_DRIVER_KEY, keySuffix);
        redisTemplate.delete(key);
    }

    @Override
    public String getOrderDriverValue(String keySuffix) {
        String key = String.format(ORDER_DRIVER_KEY, keySuffix);
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }

    @Override
    public void writePartyDriverValue(String keySuffix, String value) {
        String key = String.format(ORDER_DRIVER_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, value, 15, TimeUnit.DAYS);
    }

    @Override
    public void writeTcRideOrderGrabValue(String key, Object value) {
        redisTemplate.opsForHash().put(ORDER_TC_RIDE_GRAB_KEY, key , value);
    }

    @Override
    public Object getTcRideOrderGrabValue(String key) {
        return redisTemplate.opsForHash().get(ORDER_TC_RIDE_GRAB_KEY, key);
    }

    @Override
    public void writeLineGrabMaxNumValue(String keySuffix, Integer value) {
        String key = String.format(LINE_GRAB_NUM_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, String.valueOf(value));
    }

    @Override
    public Integer getLineGrabMaxNumValue(String keySuffix) {
        String key = String.format(LINE_GRAB_NUM_KEY, keySuffix);
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtil.isBlank(value)) {
            return -2;
        }
        return Integer.parseInt(value);
    }

    @Override
    public void writeLineNameValue(String keySuffix, String value) {
        String key = String.format(LINE_NAME_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String getLineNameValue(String keySuffix) {
        String key = String.format(LINE_NAME_KEY, keySuffix);
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }

    @Override
    public void writeVehicleTypeNameValue(String keySuffix, String value) {
        String key = String.format(VEHICLE_TYPE_KEY, keySuffix);
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String getVehicleTypeNameValue(String keySuffix) {
        String key = String.format(VEHICLE_TYPE_KEY, keySuffix);
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }

}
